package com.tongzhou.tongzhou.sales.action;

import java.io.File;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.tongzhou.sales.dao.ISalesDAO;
import com.tongzhou.tongzhou.sales.model.Sales;
import com.tongzhou.tongzhou.sales.model.SearchSalesCondition;
import com.tongzhou.util.Config;
import com.tongzhou.util.FileUtil;

public class SalesAction extends ActionSupport{
	private int id;
	private Sales sales;
	private List<Sales> result=null;
	private SearchSalesCondition condition=null;
	
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	private String title;
	private String type;
	private String status;
	private ISalesDAO dao=null;
	
	private File image_url ;
	private String image_urlFileName ;
	/**
	 * 用户列表界面
	 *@author: 彭定雄
	 *@return 
	 *@throws DBException
	 */
	public String listSales() throws DBException{
		checkSearchCondition();
		condition = new SearchSalesCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setCurrentPage(currentPage) ;
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType) ;
		condition.setPageSize(pageSize) ;
		condition.setTitle(title);
		condition.setType(type);
		condition.setStatus(status);
	    result = dao.listSales(condition);
		return "listSales";
	} 
	
	
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "id" ;
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "desc" ;
		}
	}
	
	/**
	 * 新增用户
	 *@author: 彭定雄
	 *@return
	 *@throws DBException
	 */
	public String toAddSales() throws DBException{
		return "addSales";
	}
	
	public String addSales() throws DBException{
		if(image_url!=null){
			String s=image_urlFileName.replace(".", "`").split("`")[1];
			String path = "sales"+File.separator+FileUtil.generateFileName()+"."+s;
			path=path.replace("\\","\\\\");
			FileUtil.copyFile(image_url, new File(FileUtil.getWebRootDir()+path));
			sales.setImage_url(path);
		}
		dao.addSales(sales);
		listSales();
		return "listSales";
	}
	
	public String deleteSales() throws DBException{
//		dao=new UserMgrDAO();
		dao.deleteSales(id);
		listSales();
		return "listSales";
	}
	
	public String toEditSales() throws DBException{
		sales=dao.getSalesById(id);
		return "editSales";
	}
	
	public String editSales() throws DBException{
		if(image_url!=null){
			String s=image_urlFileName.replace(".", "`").split("`")[1];
			String path = "sales"+File.separator+FileUtil.generateFileName()+"."+s;
			path=path.replace("\\","\\\\");
			FileUtil.copyFile(image_url, new File(FileUtil.getWebRootDir()+path));
			sales.setImage_url(path);
		}else{
			sales.setImage_url(sales.getImage_url().replace("\\","\\\\"));
		}
		dao.updateSales(sales);
		listSales();
		return "listSales";
	}
	
	public String toDetailSales() throws DBException{
		sales=dao.getSalesById(id);
		return "detailSales";
	}
	
	


	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public Sales getSales() {
		return sales;
	}


	public void setSales(Sales sales) {
		this.sales = sales;
	}


	public List<Sales> getResult() {
		return result;
	}


	public void setResult(List<Sales> result) {
		this.result = result;
	}


	public SearchSalesCondition getCondition() {
		return condition;
	}


	public void setCondition(SearchSalesCondition condition) {
		this.condition = condition;
	}


	public int getPageSize() {
		return pageSize;
	}


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}


	public int getCurrentPage() {
		return currentPage;
	}


	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}


	public String getOrderBy() {
		return orderBy;
	}


	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}


	public String getOrderByType() {
		return orderByType;
	}


	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}


	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}


	public String getType() {
		return type;
	}


	public void setType(String type) {
		this.type = type;
	}


	public String getStatus() {
		return status;
	}


	public void setStatus(String status) {
		this.status = status;
	}


	public ISalesDAO getDao() {
		return dao;
	}


	public void setDao(ISalesDAO dao) {
		this.dao = dao;
	}


	public File getImage_url() {
		return image_url;
	}


	public void setImage_url(File imageUrl) {
		image_url = imageUrl;
	}


	public String getImage_urlFileName() {
		return image_urlFileName;
	}


	public void setImage_urlFileName(String imageUrlFileName) {
		image_urlFileName = imageUrlFileName;
	}

}
